//
//  MXSVisitorView.swift
//  weibo
//
//  Created by 孙飞 on 15/12/7.
//  Copyright © 2015年 Sunfei. All rights reserved.
//

import UIKit
import SnapKit

class MXSVisitorView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 执行逻辑
    
    /// 供外界调用,设置图标以及消息文字的方法
    func setVisitorInfo(imgName: String?, message: String?){
        
        if let img = imgName, msg = message {
            // 只有 消息.发现.我 这三个页面,才会执行到这个地方
            // 隐藏 圆圈
            circleView.hidden = true
            iconView.image = UIImage(named: img)
            messageLabel.text = msg
        }else{
            startAnim()
        }
    }
    
    
    private lazy var iconView: UIImageView = {
        let image = UIImage(named: "visitordiscover_feed_image_house")
        let imageView = UIImageView( image: image)
        return imageView
    }()
    
    private lazy var maskCircleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
    //转动的圆
    
//    private lazy var circleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var circleView: UIImageView = {
        return UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
    }()
    
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.numberOfLines = 0
        return label
    }()
    
    //不需要暴露给外部的属性，私有
    // MARK: - 注册和登陆按钮 外部需要调用
    lazy var registerBtn: UIButton = {
        let button = UIButton()
        button.setTitle("注册", forState: .Normal)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable")?.stretchableImageWithLeftCapWidth(5, topCapHeight: 5), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        return button
    }()
    
    
    lazy var loginBtn: UIButton = {
        let button = UIButton()
        button.setTitle("登陆", forState: .Normal)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable")?.stretchableImageWithLeftCapWidth(5, topCapHeight: 5), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        return button
    }()
    
   

}
//mark: - 
extension MXSVisitorView{
    
    private func setupUI(){
        
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        //空间叠加次序
        addSubview(circleView)
        addSubview(maskCircleView)
        addSubview(iconView)
        
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        circleView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        iconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleView.snp_center)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(circleView.snp_bottom).offset(16)
            make.centerX.equalTo(circleView.snp_centerX)
            make.width.equalTo(224)
        }
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSizeMake(100, 35))
        }
        // 登录
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerBtn)
            make.size.equalTo(CGSizeMake(100, 35))
        }
        // 半透明遮罩
        maskCircleView.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(registerBtn.snp_bottom)
        }
        
        startAnim()
    }
    
    private func startAnim(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        
        circleView.layer.addAnimation(anim, forKey: nil)
    }
}






